clear


/* First session */
insheet using "${pathdata_robustness}/RobustnessExperiment4_raw_wave1.csv", names
drop in 1/2

drop if writedowninfo=="1"
destring *, replace 

/* Rename and merge belief variables */
rename _q10 beliefs_count_videogame
rename _q232 beliefs_percentage_videogame
rename _q230_pagesubmit p_guess_time_videogame

rename v37 beliefs_count_restaurant
rename v38 beliefs_percentage_restaurant
rename v41 p_guess_time_restaurant

rename v43 beliefs_count_bicycle
rename v44 beliefs_percentage_bicycle
rename v47 p_guess_time_bicycle

foreach product in videogame restaurant bicycle {
    replace p_guess_`product' = beliefs_count_`product'
	replace p_guess_`product' = beliefs_percentage_`product' if missing(p_guess_`product')
}


/* Variables to keep */
keep condition_* beliefs_type statistics_type p_guess* task* p_guess_time* prolific_pid positive_stat negative_stat  tot_reviews* story_type* qid9 qid124 qid12 qid18 durationinseconds comprehension1 comprehension2 comprehension3 comprehension5 q202 screenedout writedowninfo

rename qid9 gender 

rename qid124 age

rename qid12 education

rename qid18 employment

* Indicator Male
gen male = .
replace male = 0 if inlist(gender, 2,3)
replace male = 1 if gender == 1

* Indicator Bachelor's degree or more
gen college = .
replace college = 0 if inlist(education, 1,2,3,4)
replace college = 1 if inlist(education, 5,6,7,8)

* Indicator Employment status
	gen employed = .
	replace employed = 0 if inlist(employment,3,4)
	replace employed = 1 if inlist(employment,1,2)

duplicates drop prolific_pid, force

reshape long condition_ p_guess_ task_ p_guess_time_, i(prolific_pid) j(product) string



save "${pathdata_robustness}/temp/RobustnessExperiment4_long.dta", replace


duplicates drop prolific_pid, force
drop if prolific_pid==""
keep prolific_pid beliefs_type statistics_type


save "${pathdata_robustness}/temp/RobustnessExperiment4_prolific_pid_only.dta", replace

clear

/* Second (recall) session */
insheet using "${pathdata_robustness}/RobustnessExperiment4_raw_wave2.csv", names
drop in 1/2

destring *, replace

gen valence_time_videogame = _q646_pagesubmit
gen valence_time_bicycle = v42
gen valence_time_restaurant = v34

/*NOTE: There is a problem somewhere in this part, but the original coded valence variables are fine */
/* Rename and merge valence variables */

/*
drop valence_bicycle valence_restaurant valence_videogame

rename _valence_story valence_story_videogame
rename _valence_stat valence_stat_videogame
rename _valence_dontremember valence_dontremember_videogame
rename _q646_pagesubmit valence_time_videogame

rename v29 valence_story_restaurant
rename v30 valence_stat_restaurant
rename v31 valence_dontremember_restaurant
rename v34 valence_time_restaurant

rename v37 valence_story_bicycle
rename v38 valence_stat_bicycle
rename v39 valence_dontremember_bicycle
rename v42 valence_time_bicycle

foreach product in videogame restaurant bicycle {
    gen valence_`product' = valence_story_`product'
	replace valence_`product' = valence_stat_`product' if missing(valence_`product')
	replace valence_`product' = valence_dontremember_`product' if missing(valence_`product')
}
*/
/* Rename and merge belief variables */
rename _q633 beliefs_count_videogame
rename _q334 beliefs_percentage_videogame
rename _q634_pagesubmit beliefs_time_videogame

rename v50 beliefs_count_restaurant
rename v51 beliefs_percentage_restaurant
rename v54 beliefs_time_restaurant

rename v56 beliefs_count_bicycle
rename v57 beliefs_percentage_bicycle
rename v60 beliefs_time_bicycle

foreach product in videogame restaurant bicycle {
    replace beliefs_`product' = beliefs_count_`product'
	replace beliefs_`product' = beliefs_percentage_`product' if missing(beliefs_`product')
}

/* Rename and merge "type of information" variables */

rename _typeofinfo typeofinfo_videogame
rename v28 typeofinfo_restaurant
rename v36 typeofinfo_bicycle

/* Variables to keep */
keep valence_bicycle valence_restaurant valence_videogame beliefs_bicycle beliefs_restaurant beliefs_videogame recall_bicycle recall_restaurant recall_videogame valence_time_* beliefs_time_* typeofinfo_* prolific_pid screenedout

drop if prolific_pid == ""
sort prolific_pid
quietly by prolific_pid:  gen dup = cond(_N==1,0,_n)
drop if dup > 0
drop dup  

/* Wide to long */
reshape long beliefs_ valence_ recall_ valence_time_ beliefs_time_ typeofinfo_, i(prolific_pid) j(product) string

save "${pathdata_robustness}/temp/RobustnessExperiment4_long_followup.dta", replace

/* Merged */
merge 1:1 prolific_pid product using "${pathdata_robustness}/temp/RobustnessExperiment4_long.dta"

* ########## CREATE DATA ON ATTRITION IN BETWEEN SUBJECT TREATMENTS ##########	

	/*
	* indicator whether subjects completed study 2
	gen completed = 0
	replace completed = 1 if _merge ==3
	
	* betweeen subject treatment indicators
	gen belief_percent = 0
	replace belief_percent = 1 if beliefs_type == "percentage"
	
	gen info_percent = 0
	replace info_percent = 1 if statistics_type == "percentage"
	
	*/
	* indicator whether subjects completed study 2
	gen completed = 0
	replace completed = 1 if _merge ==3
	
	* betweeen subject treatment indicators
	gen belief_percent = 0
	replace belief_percent = 1 if beliefs_type == "percentage"
	
	gen info_percent = 0
	replace info_percent = 1 if statistics_type == "percentage"
	
	* ##### Generate dataset with treatments and completion only
	


	
	* ##### Generate dataset with treatments and completion only
preserve
	* drop NAs
	

	drop if _merge==1
	
	* REMOVE SUBJECTS AFFECTED BY CODING ERROR
	gen fraction_pos = positive_stat/tot_reviews_pos if inlist(condition_, "statistic_pos", "statistic_pos_prompt")
	drop if inlist(condition_, "statistic_pos", "statistic_neg") & fraction_pos == 1

	bysort prolific_pid : drop if _N < 3
	
	drop if p_guess_ == .

	keep prolific_pid completed info_percent belief_percent
	
	duplicates drop
	
	
	
	save "${pathdata_summary}/FormatAttrition.dta", replace
	
restore
	
	drop info_percent belief_percent



keep if _merge==3
drop _merge

destring*, replace


/* Exclusion Criteria */
drop if screenedout == "True"
drop if condition_ == "noinfo" & p_guess_ != 50

save "${pathdata_robustness}/temp/RobustnessExperiment4_merged.dta", replace



/* Variables for analysis */

/* 1 - Effect variables */
gen diff = 50 - p_guess_
gen diff_recall = 50 - beliefs_


gen effect = diff
replace effect = p_guess_ - 50 if inlist(condition_, "storyshort_pos", "statistic_pos", "storyplacebo_pos", "storyplacebo_pos") 

gen effect_recall = diff_recall
replace effect_recall = beliefs_ - 50 if inlist(condition_, "storyshort_pos", "statistic_pos", "storyplacebo_pos", "storyplacebo_pos")


/* Valence recall */
gen valence_recall = 0
* if constraining valence recall to those who actually were asked to recall valence, i.e. who did not select noinfo as type of information remembered
//replace valence_recall = . if valence_ == .
replace valence_recall = 1 if valence_ == 1 & inlist(condition_, "statistic_pos",  "storyshort_pos")
replace valence_recall = 1 if valence_ == 2 & inlist(condition_, "statistic_neg", "storyshort_neg")

/* Recall type of information */
gen type_recall = 0
replace type_recall = . if recall_ == .
replace type_recall =  1 if condition_=="noinfo" & recall_ ==1
replace type_recall =  1 if inlist(condition_, "statistic_pos", "statistic_neg", "statistic_pos_prompt", "statistic_neg_prompt") & recall_ == 2
replace type_recall =  1 if inlist(condition_, "storyplacebo_pos", "storyplacebo_neg", "storyshort_neg", "storyshort_pos", "storyplacebo_pos_prompt", "storyplacebo_neg_prompt") & recall_ ==3


/* Combined recall */
gen combined_recall = 0
* if constraining to those who actually were asked to recall valence, i.e. who did not select noinfo as type of information remembered
replace combined_recall = . if valence_recall ==.
replace combined_recall = 1 if type_recall == 1 & valence_recall == 1
//replace combined_recall = . if condition == "noinfo"


/* Drop missing products */
drop if p_guess_ == .

*tab recall_ treatment , m
*drop if recall_ == .

gen fraction_pos = positive_stat/tot_reviews_pos if inlist(condition_, "statistic_pos", "statistic_pos_prompt")
gen fraction_neg = negative_stat/tot_reviews_neg if inlist(condition_, "statistic_neg", "statistic_neg_prompt")

gen fraction_signal = fraction_pos
replace fraction_signal = fraction_neg if fraction_signal==.

gen extremity = abs(fraction_signal - 0.5)

// /*Define objects of interest for graphs*/
// gen typestat=.
// replace typestat = 1 if inlist(condition_, "statistic_pos", "statistic_neg") & treatment == 1
// replace typestat = 2 if inlist(condition_, "statistic_pos_prompt", "statistic_neg_prompt") & treatment == 2
//
// tab recall typestat
//
// tab condition_
//
// gen typestory=.
// replace typestory = 1 if inlist(condition_, "storyplacebo_pos", "storyplacebo_neg")
// replace typestory = 2 if inlist(condition_, "storyplacebo_pos_prompt", "storyplacebo_neg_prompt")

drop if condition_ == "noinfo"

/* Drop observations where updating is in the wrong direction */
//tab effect
drop if effect < 0 & inlist(condition_, "statistic_pos", "statistic_neg")



* Some analyses
encode beliefs_type, gen(beliefs_type_)
encode statistics_type, gen(statistics_type_)

anova p_guess_time_ beliefs_type_ statistics_type_ beliefs_type_#statistics_type_
anova beliefs_time_ beliefs_type_ statistics_type_ beliefs_type_#statistics_type_


* REMOVE SUBJECTS AFFECTED BY CODING ERROR
drop if inlist(condition_, "statistic_pos", "statistic_neg") & fraction_pos == 1


bysort prolific_pid : drop if _N < 2

save "${pathdata_robustness}/RobustnessExperiment4.dta", replace


preserve
* ##### Save data for summary statistics

	keep prolific_pid male age college employed

	duplicates drop

	save "${pathdata_summary}/FormatSummary.dta", replace
	
restore
